package pattern.builder.car;

import pattern.builder.car.impl.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 建造者模式
 * 将一些不会变化的子单元，组合成不同类型的组件（builder）
 * @author ytj
 * @date 2025/8/6
 */
public class CarPackageBuild implements CarPartBuild {
    private List<ICarParts> list=new ArrayList<>();
    private String carName;
    private BigDecimal total=BigDecimal.ZERO;
    public CarPackageBuild(){

    }
    public CarPackageBuild(String carName){
        this.carName=carName;
    }
    @Override
    public CarPartBuild appendBattery(ICarParts carParts) {
        list.add(carParts);
        total=total.add(carParts.price());
        return this;
    }

    @Override
    public CarPartBuild appendTire(ICarParts carParts) {
        list.add(carParts);
        total=total.add(carParts.price());
        return this;
    }

    @Override
    public CarPartBuild appendFrame(ICarParts carParts) {
        list.add(carParts);
        total=total.add(carParts.price());
        return this;
    }
    @Override
    public void desc(){
        System.out.println("汽车名称："+carName+",总造价:"+total);
        for (ICarParts iCarParts : list) {
            System.out.println("组件名称："+iCarParts.name()+"，品牌："+iCarParts.brand()+"，价格："+iCarParts.price());
        }
        System.out.println("-----------------------------------------------------");
    }

    public static void main(String[] args) {
        new CarPackageBuild("比亚迪").appendBattery(new BydBattery()).appendTire(new ContinentalTire()).appendFrame(new IronFrame()).desc();

        new CarPackageBuild("问界").appendBattery(new NingBattery()).appendTire(new MichelinTire()).appendFrame(new AluminiumFrame()).desc();
    }
}
